package com.core.cloudcommon;

/**
 * @author =====Lqj=====
 * @className CommonResultCode
 * @description 公共响应编码
 * @create 2024/11/13 20:38
 **/

public enum CommonResultCode {
    CODE200(200, "操作成功!"),
    CODE400(400, "无效请求，请重试!"),
    CODE401(401, "您无权访问!"),
    CODE403(403, "禁止访问!"),
    CODE404(404, "资源未找到!"),
    CODE500(500, "服务内部发生未知错误,请联系管理员!"),

    // 链路追踪相关错误码 (800-899)
    CODE800(800, "链路追踪ID不能为空!"),
    CODE801(801, "链路信息不存在!"),
    CODE802(802, "链路日志查询失败!"),
    CODE803(803, "链路报告生成失败!"),
    CODE804(804, "链路ID格式不正确!"),
    CODE805(805, "链路查询参数无效!");

    private final Integer code;
    private final String msg;

    CommonResultCode(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    // 获取 code 值
    public Integer getCode() {
        return code;
    }

    // 获取 msg 值
    public String getMsg() {
        return msg;
    }

    /**
     * 通过 code 获取枚举项
     */
    public static CommonResultCode getByCode(Integer code) {
        if (code == null) {
            throw new IllegalStateException("响应编码不能为空");
        }
        for (CommonResultCode resultCode : CommonResultCode.values()) {
            if (resultCode.getCode().equals(code)) {
                return resultCode;
            }
        }
        throw new IllegalStateException("编码不存请重试输入,不存在的编码：" + code);
    }


}
